/*
 * @Author: your name
 * @Date: 2022-04-15 15:53:56
 * @LastEditTime: 2022-04-18 17:13:56
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \mallAdmin\src\app\sharedModule\commponent\city-select\city-select.component.ts
 */
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, SimpleChanges } from '@angular/core';
import cityData from '../../../services-common/city';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';

interface OutInfoData {
  provinceId: number,
  provinceName: string,
  cityId: number,
  cityName: string,
  countyId: number,
  countyName: string
}

@Component({
  selector: 'app-city-select',
  templateUrl: './city-select.component.html',
  styleUrls: ['./city-select.component.scss']
})
export class CitySelectComponent implements OnInit, OnChanges {

  @Input() countyId!: number | string; // 区ID

  @Output() cityChange = new EventEmitter();
  
  selectedCity: number[] = []; // 级联Array

  loadData(node: NzCascaderOption, index: number): PromiseLike<void> {
    return new Promise(resolve => {
      if (index < 0) {
        // if index less than 0 it is root node
        node.children = cityData.province;
      } else if (index === 0) {
        node.children = (cityData as any).city[node.value];
      } else {
        node.children = (cityData as any).county[node.value];
      }
      resolve();
    });
  }

  constructor() { }

  ngOnInit(): void {
    // if (this.countyId) {
    //   let regionInfos: any = this.cityFilter(this.countyId);
    //   this.selectedCity = [regionInfos.provinceId, regionInfos.cityId, regionInfos.countyId];
    // }
  }

  ngOnChanges(changes: SimpleChanges): void {
    // console.log('change val', changes);
    if (this.countyId) {
      let regionInfos: any = this.cityFilter(this.countyId);
      this.selectedCity = [regionInfos.provinceId, regionInfos.cityId, regionInfos.countyId];
    }
  }

  onChanges(values: number[]): void {
    if (values && values.length) {
      let {provinceId, provinceName, cityId, cityName, countyId, countyName}: OutInfoData = this.cityFilter(values[2]);
      this.cityChange.emit({
        valueArrs: [provinceId, cityId, countyId],
        labelArrs: [provinceName, cityName, countyName],
        labelStr: [provinceName, cityName, countyName].join(','),
        countyId: countyId
      });
    } else {
      this.cityChange.emit({
        message: '暂无匹配数据...'
      });
    }
  }

  cityFilter(countyId: number | string) {
    let regionObjs: any = cityData || {};
    let objs: any;
    for (const key in regionObjs.province) {
      let province = regionObjs.province[key];
      // 市
      for (const k in regionObjs.city[province.value]) {
        let city = regionObjs.city[province.value][k];
        // 区
        for (let index = 0; index < regionObjs.county[city.value].length; index++) {
          let county = regionObjs.county[city.value][index];
          if (county.value === countyId) {
            objs = {
              provinceId: province.value,
              provinceName: province.label,
              cityId: city.value,
              cityName: city.label,
              countyId: county.value,
              countyName: county.label
            }
            break;
          }
        }
      }
    }
    return objs;
  }
}
